package com.share.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.common.security.utils.SecurityUtils;
import com.share.domain.MySql.PowerBank;
import com.share.service.PowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Tag(name = "充电宝管理业务")
@RestController
@RequestMapping("/powerBank")
public class PowerBankController extends BaseController {


    @Autowired
    PowerBankService powerBankService;


    /**
     * 分页查询充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "分页查询充电宝接口")
    @RequiresPermissions("device:powerBank:list")
    @GetMapping("/list")
    public TableDataInfo list(PowerBank powerBank) {
        // 初始化分页参数
        startPage();
        // 调用服务层方法，根据条件查询充电宝列表
        List<PowerBank> list = powerBankService.selectPowerBankList(powerBank);
        // 将查询结果封装到TableDataInfo对象中并返回
        return getDataTable(list);
    }


    /**
     * 获取充电宝详细信息接口
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取充电宝详细信息接口")
    @RequiresPermissions("device:powerBank:query")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        PowerBank powerBank = powerBankService.getById(id);
        return success(powerBank);
    }


    /**
     * 新增充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "新增充电宝接口")
    @Log(title = "充电宝管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("device:powerBank:add")
    @PostMapping
    public AjaxResult add(@RequestBody PowerBank powerBank) {
        //插入创建者信息
        powerBank.setCreateBy(SecurityUtils.getUsername());
        //插入创建时间
        powerBank.setCreateTime(new Date());
        powerBank.setUpdateTime(new Date());
        return toAjax(powerBankService.savePowerBank(powerBank));
    }



    /**
     * 修改充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Operation(summary = "修改充电宝接口")
    @Log(title = "充电宝管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("device:powerBank:edit")
    @PutMapping
    public AjaxResult update(@RequestBody PowerBank powerBank) {
        //插入修改者信息
        powerBank.setUpdateBy(SecurityUtils.getUsername());
        //插入修改时间
        powerBank.setUpdateTime(new Date());
        return toAjax(powerBankService.updatePowerBank(powerBank));
    }



    /**
     * 删除充电宝
     * @param ids
     * @return
     */
    @Operation(summary = "删除充电宝接口")
    @Log(title = "充电宝管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("device:powerBank:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable Long[] ids){
        boolean removeBatchByIds = powerBankService.removeBatchByIds(Arrays.asList(ids));
        return toAjax(removeBatchByIds);
    }






}
